package com.py.credit.utils;


import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {
    public static String getnow() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        return df.format(new Date());
    }

    public static String getCurrentTime() {
        Date dt = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate = formatter.format(dt);
        return currentDate.trim();
    }

    public static String getCurrentTime(String format) {
        Date dt = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String currentDate = formatter.format(dt);
        return currentDate.trim();
    }

    public static String getDateToStr(Date dt, String format) {
        if (dt == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            String dateString = formatter.format(dt);
            return dateString.trim();
        }
    }

    public static Date getStrToDate(String strDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        Date dt = formatter.parse(strDate, pos);
        return dt;
    }



    public static String getLongToStr(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(time * 1000L).trim();
    }

    public static String getWeekOfDate() {
        Date dt = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy 年 MM 月 dd 日");
        String currentDate = formatter.format(dt);
        String[] weekDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(7) - 1;
        if (w < 0) {
            w = 0;
        }

        return currentDate + " " + weekDays[w];
    }

    public static String getWeekOfToday(Date dt) {
        if (dt == null) {
            dt = new Date();
        }

        String[] weekDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(7) - 1;
        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    public static int getWeekOfDayint(Date dt) {
        if (dt == null) {
            dt = new Date();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(7);
        --w;
        if (w == 0) {
            w = 7;
        }

        return w;
    }

    public static String getWeekOfYear(Date dt) {
        if (dt == null) {
            dt = new Date();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        cal.setFirstDayOfWeek(2);
        int w = cal.get(3);
        return cal.get(1) + "-" + (w > 9 ? w : "0" + w) + "-" + getWeekOfDayint(dt);
    }

    public static Date getaddDate(Date dt, int type, int value) {
        if (dt == null) {
            dt = new Date();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        cal.add(type, value);
        return cal.getTime();
    }
}
